//
//  TextFormatingMini.m
//  IOS_SDK
//
//  Created by Tzvi on 8/25/11.
//  Copyright 2011 STAR MICRONICS CO., LTD. All rights reserved.
//

#import "AppDelegate.h"

#import "TextFormatingMini.h"
#import <QuartzCore/QuartzCore.h>
#import "StandardHelp.h"
#import "MiniPrinterFunctions.h"
#import "nibLoader.h"

@implementation TextFormatingMini

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [uiview_main addSubview:uiscrollview_main];
    [uiscrollview_main setContentSize: CGSizeMake(320, 602)];
    [uiscrollview_main setScrollEnabled:TRUE];
    [uiscrollview_main setFrame:CGRectMake(0, 0, 320, 460)];
}
- (IBAction)backTextFormating
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [uiscrollview_main setFrame:CGRectMake(0, 0, 320, 230)];
    [uiscrollview_main setScrollEnabled:TRUE];
    [uiscrollview_main setContentOffset:CGPointMake(0, 100) animated:TRUE];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [uiscrollview_main setFrame:CGRectMake(0, 0, 320, 230)];
    [uiscrollview_main setScrollEnabled:TRUE];
    [uiscrollview_main setContentOffset:CGPointMake(0, 300) animated:TRUE];
}


- (IBAction)printTextFormating
{
    NSString *portName = [AppDelegate getPortName];
    NSString *portSettings = [AppDelegate getPortSettings];
    
    bool underline = false;
    bool emphasized = false;
    bool upsidedown = false;
    bool inverColor = false;
    
    unsigned char heightExpansion = 0;
    unsigned char widthExpansion = 0;
    int leftMargin = 0;
    
    Alignment alignment = Left;
    NSData *textData = [uitextview_texttoprint.text dataUsingEncoding:NSWindowsCP1252StringEncoding];
    
    unsigned char *textBytes = (unsigned char*)malloc([textData length]);
    [textData getBytes:textBytes];
    
    [MiniPrinterFunctions PrintText:portName 
                        PortSettings:portSettings 
                        Underline:underline 
                        Emphasized:emphasized
                        Upsideddown:upsidedown
                        InvertColor:inverColor
                        HeightExpansion:heightExpansion
                        WidthExpansion:widthExpansion
                        LeftMargin:leftMargin
                        Alignment:alignment
                        TextToPrint:textBytes
                    TextToPrintSize:[textData length]];
    
    free(textBytes);
}


- (IBAction)pushButtonSearch:(id)sender
{
    [searchView removeFromSuperview];
    
    searchView = [nibLoader load:@"PrinterSearchView" params:nil];
    searchView.center = CGPointMake(160, 480);
    searchView.name_label = uilabel_portname;
    [self.view addSubview:searchView];
}
@end
